package com.topscore.uke.ui.fragments;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.kingja.loadsir.callback.Callback;
import com.kingja.loadsir.core.LoadService;
import com.kingja.loadsir.core.LoadSir;
import com.topscore.uke.R;
import com.topscore.uke.entities.TeamMemberItem;
import com.topscore.uke.mvp.contractors.GetTeamStaffListContractor;
import com.topscore.uke.mvp.presenters.GetTeamStaffListPresenter;
import com.topscore.uke.ui.BaseSupportFragment;
import com.topscore.uke.ui.activities.MyTeamStaffListActivity;
import com.topscore.uke.ui.loadsir_callback.ErrorCallBack;
import com.topscore.uke.ui.loadsir_callback.LoadingCallBack;
import com.topscore.uke.utils.SnackBarUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * A simple {@link Fragment} subclass.
 */
public class StaffSearchResultFragment extends BaseSupportFragment implements MyTeamStaffListActivity.OnSearchStaffListener, GetTeamStaffListContractor.View {

    @Bind(R.id.recycler_view)
    RecyclerView recyclerView;

    private View rootView;

    private BaseQuickAdapter<TeamMemberItem, BaseViewHolder> mAdapter;

    private View mEmptyView;

    private GetTeamStaffListContractor.Presenter mSearchPresenter;

    private LoadService mLoadService;

    public StaffSearchResultFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        rootView = inflater.inflate(R.layout.fragment_staff_search_result, container, false);
        ButterKnife.bind(this, rootView);
        initViews();
        return rootView;
    }

    private void initViews() {
        mLoadService = LoadSir.getDefault().register(recyclerView, new Callback.OnReloadListener() {
            @Override
            public void onReload(View v) {

            }
        });
        mEmptyView = LayoutInflater.from(mContext).inflate(R.layout.empty_view, (ViewGroup) recyclerView.getParent(), false);
        ((TextView) mEmptyView.findViewById(R.id.empty_message)).setText("暂无搜索结果");
        mAdapter = new BaseQuickAdapter<TeamMemberItem, BaseViewHolder>(R.layout.team_staff_item, new ArrayList<>()) {
            @Override
            protected void convert(BaseViewHolder holder, TeamMemberItem item) {
                holder.setText(R.id.name_tv, item.getMemberName());
            }
        };
        mAdapter.setEmptyView(mEmptyView);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        recyclerView.setAdapter(mAdapter);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onSearch(String memberName) {
        if (mSearchPresenter == null) {
            mSearchPresenter = new GetTeamStaffListPresenter(this);
        }

        mSearchPresenter.searchStaffMember(memberName);
    }

    @Override
    public void onLoading(String loadingMessage) {
        mLoadService.showCallback(LoadingCallBack.class);
    }

    @Override
    public void onLoadFinished() {
        mLoadService.showSuccess();
    }

    @Override
    public void onFail(int code, String errorMessage) {
        SnackBarUtil.getInstance(rootView).showShort(errorMessage).show();
        mLoadService.showCallback(ErrorCallBack.class);
    }

    @Override
    public void onGetDataSuccess(List<TeamMemberItem> data) {
        mAdapter.setNewData(data);
    }
}
